####
#
# Copyright 2007 B. Chandler Carruth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
####
#
# ./lib/rcons/file_target.rb
#
# This file provides the basic file target functionality.
#
####

require( 'rcons/target' )
require( 'rcons/util/file_change_module' )

module RCons
  # Provides basic file functionality.
  #
  # An example of a child class:  +SourceFileTarget+.
  #
  # Extends +Target+.
  class FileTarget < Target
    attr_reader( :file_name )

    # initialize a simple command target
    # Initializes a file target.
    #
    # Inputs:
    #
    # +build_env+:: an +Environment+.
    # +name+:: the name of the file target, not to be confused with the filename.
    # +opts+:: a list of options.
    def initialize( build_env, name, opts = Hash.new() )
      # set up name as a file
      unless name[ 0 ] == '~' or name == File.expand_path( name )
        name = File.join( '$(BUILD_ROOT)', name )
      end

      @file_name = name

      # handle all the defaults for a target
      super( build_env,
             File.join( 'file', File.expand_path( build_env.expand( @file_name ) ) ),
             opts )

      @builder.file_name = @file_name
    end

    # The FileTarget overriden Builder class.
    class Builder < superclass()::Builder
      include FileChangeModule

      attr_accessor( :file_name )

      # This is the custom method for building the target
      def do_build( has_changed )
        unless check_digest( @file_name )
          unless File.exists?( @build_env.expand( @file_name ) )
            @build_env.print( "File '#{@build_env.expand( @filename )}' does not exist!" )
            return false
          end

          update_digest( @file_name )
          super( true )
        else
          super( false )
        end
      end
    end
  end
end

